package org.skydiveapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import org.skydiveapplibrary.repository.AircraftRepository;
import org.skydiveapplibrary.ui.AircraftListAdapter;

public class AircraftListActivity extends BaseListActivity
{
	// menus
	private MenuItem addMenu;
	
    @Override 
    public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	
    	getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
    	setHeaderText(R.string.aircrafts_label);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	loadData();
    }
    
    @Override
    protected void onListItemClick(ListView lv, View view, int position, long id)
    {
    	Intent intent = new Intent(this, AircraftEditActivity.class);
		intent.putExtra(Constants.EXTRA_AIRCRAFT_ID, (int)id);
    	startActivity(intent);
    }
    
    private void loadData()
    {
    	// populate aircrafts
		AircraftRepository repository = new AircraftRepository(this);
		AircraftListAdapter adapter = new AircraftListAdapter(
				this,
				repository.getAircrafts());
		setListAdapter(adapter);
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		addMenu = menu.add(R.string.add_button);
		addMenu.setIcon(R.drawable.ic_menu_add);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (item == addMenu)
		{
        	Intent intent = new Intent(this, AircraftEditActivity.class);
        	startActivity(intent);
    		return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}
} 